package com.ucar.umock.client.util;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Scanner;

/**
 * @ClassName MyMockFreemarkerUtil
 * @Description TODO
 * @Author zhanbs baoshan.zhan@ucarinc.com
 * @Date 2020/4/2 17:30
 * @Version 1.0
 **/
public class UMockFreemarkerUtil {

    protected static Logger logger = LoggerFactory.getLogger(UMockFreemarkerUtil.class);

    private static StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    private static Configuration stringConfiguration=new Configuration(Configuration.VERSION_2_3_27);
    static{
        stringConfiguration.setTemplateLoader(stringTemplateLoader);
    }

    /**
     *
     * @param name 模板字符串KEY
     * @param templateContent 模板字符串内容
     * @param root
     * @return
     * 2017年7月25日 下午3:19:36
     * @author:詹保山
     */
    private static String doStringTemplateResolver(String name,String templateContent, Map<String, Object> root){
        if(StringUtils.isBlank(templateContent)){
            return "";
        }
        //加入公共的指令
        Object tmpObj=stringTemplateLoader.findTemplateSource(name);
        if(tmpObj==null){
            stringTemplateLoader.putTemplate(name, templateContent);
        }else {
            if(!templateContent.equals(tmpObj.toString())) {//不一样的情况下
                stringTemplateLoader.putTemplate(name, templateContent);
            }
        }
        Template temp;
        try {
            temp= stringConfiguration.getTemplate(name,"utf-8");
            Writer writer=new StringWriter();
            temp.process(root, writer);
            writer.flush();
            writer.close();
            return writer.toString();
        } catch (IOException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        } catch (TemplateException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 从jar包中加载File文件内容
     * @param fileName
     * @return
     */
    private static String loadFileContentFromJar(final String fileName) {
        InputStream inputStream = UMockFreemarkerUtil.class.getClassLoader().getResourceAsStream(fileName);
        Scanner scan = new Scanner(inputStream);
        scan.useDelimiter("\n");
        StringBuilder sb=new StringBuilder();
        while (scan.hasNextLine()){
            sb.append(scan.nextLine());
        }
        return sb.toString();
    }


    public static String parseHtml(String filePath,Map<String,Object> rootMap){
        String templateCnt=loadFileContentFromJar(filePath);
        String cnt=doStringTemplateResolver(filePath,templateCnt,rootMap);
        return cnt;
    }

}
